package com.tg.app.fragment;

import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;

import androidx.annotation.NonNull;

import com.tg.app.R;

import java.util.Objects;

/**
 * Encapsulate dialog behavior with blur effect for app using {@link android.app.DialogFragment}.
 * <p/>
 * All the screen behind the dialog will be blurred except the action bar.
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class BlurDialogFragment extends DialogFragment {
    private BlurDialogListener mBlurDialogListener;

    public static BlurDialogFragment newInstance() {
        return new BlurDialogFragment();
    }

    public void setBlurDialogListener(BlurDialogListener blurDialogListener) {
        mBlurDialogListener = blurDialogListener;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        View view = getActivity().getLayoutInflater().inflate(R.layout.scan_new_qrcode_uuid_pop, null);
        if (mBlurDialogListener != null){
            mBlurDialogListener.initView(view);
        }
        builder.setView(view);
        return builder.create();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();

        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        Objects.requireNonNull(getDialog().getWindow()).setLayout(dm.widthPixels, getDialog().getWindow().getAttributes().height);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        //getDialog().setCancelable(false);//这个会屏蔽掉返回键
        getDialog().setCanceledOnTouchOutside(true);
    }

    public interface BlurDialogListener{
        void initView(View view);
    }
    
}
